home *** CD-ROM | disk | FTP | other *** search
/ Inside Multimedia 1994 April / Inside Multimedia CD-ROM (April 1994).iso / prg / gs / gssource.exe / ZFONT2.C < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-24  |  6.5 KB  |  210 lines

  1. /* Copyright (C) 1989, 1992 Aladdin Enterprises.  All rights reserved.
  2.    Distributed by Free Software Foundation, Inc.
  3.  
  4. This file is part of Ghostscript.
  5.  
  6. Ghostscript is distributed in the hope that it will be useful, but
  7. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  8. to anyone for the consequences of using it or for whether it serves any
  9. particular purpose or works at all, unless he says so in writing.  Refer
  10. to the Ghostscript General Public License for full details.
  11.  
  12. Everyone is granted permission to copy, modify and redistribute
  13. Ghostscript, but only under the conditions described in the Ghostscript
  14. General Public License.  A copy of this license is supposed to have been
  15. given to you along with Ghostscript so you can know your rights and
  16. responsibilities.  It should be in a file named COPYING.  Among other
  17. things, the copyright notice and this notice must be preserved on all
  18. copies.  */
  19.  
  20. /* zfont2.c */
  21. /* Font creation operator for Ghostscript */
  22. #include "ghost.h"
  23. #include "errors.h"
  24. #include "oper.h"
  25. #include "gxfixed.h"
  26. #include "gsmatrix.h"
  27. #include "gxdevice.h"
  28. #include "gschar.h"
  29. #include "gxfont.h"
  30. #include "alloc.h"
  31. #include "bfont.h"
  32. #include "dict.h"
  33. #include "dparam.h"
  34. #include "name.h"
  35. #include "packed.h"
  36. #include "store.h"
  37.  
  38. /* Global font-related objects */
  39. /* Names of system-known keys in font dictionaries: */
  40. static ref name_FontType;
  41. static ref name_WMode;
  42. static ref name_FontBBox;
  43. static ref name_Encoding;
  44. ref name_UniqueID;            /* used by zfont1 */
  45. static ref name_BuildChar;
  46.  
  47. /* The global font directory */
  48. extern gs_font_dir *ifont_dir;
  49.  
  50. /* Initialize the font building operators */
  51. private void
  52. zfont2_init()
  53. {    static const names_def fnd2[] = {
  54.  
  55.     /* Create the names of the standard elements of */
  56.     /* a font dictionary. */
  57.        { "FontType", &name_FontType },
  58.        { "WMode", &name_WMode },
  59.        { "FontBBox", &name_FontBBox },
  60.        { "Encoding", &name_Encoding },
  61.        { "UniqueID", &name_UniqueID },
  62.        { "BuildChar", &name_BuildChar },
  63.  
  64.     /* Mark the end of the initalized name list. */
  65.        names_def_end
  66.     };
  67.  
  68.     init_names(fnd2);
  69. }
  70.  
  71. /* .buildfont3 */
  72. /* Build a type 3 (user-defined) font. */
  73. int
  74. zbuildfont3(os_ptr op)
  75. {    int code;
  76.     ref *pbuildchar;
  77.     gs_font *pfont;
  78.     check_type(*op, t_dictionary);
  79.     code = dict_find(op, &name_BuildChar, &pbuildchar);
  80.     if ( code <= 0 ) return e_invalidfont;
  81.     check_proc(*pbuildchar);
  82.     return build_gs_simple_font(op, &pfont, ft_user_defined, (const ref *)pbuildchar);
  83. }
  84.  
  85. /* ------ Initialization procedure ------ */
  86.  
  87. op_def zfont2_op_defs[] = {
  88.     {"1.buildfont3", zbuildfont3},
  89.     op_def_end(zfont2_init)
  90. };
  91.  
  92. /* ------ Subroutines ------ */
  93.  
  94. /* Do the common work for building a font of any non-composite FontType. */
  95. /* The caller guarantees that *op is a dictionary. */
  96. int
  97. build_gs_simple_font(os_ptr op, gs_font **ppfont, font_type ftype, const ref *pbuildchar)
  98. {    ref *pbbox;
  99.     float bbox[4];
  100.     long unique_id;
  101.     ref *puniqueid;
  102.     int code;
  103.     gs_font *pfont;
  104.     if ( dict_find(op, &name_FontBBox, &pbbox) <= 0 )
  105.       return e_invalidfont;
  106.     switch ( r_type(pbbox) )
  107.        {
  108.     default: return e_invalidfont;
  109.     case t_array: case t_mixedarray: case t_shortarray: ;
  110.         if ( r_size(pbbox) != 4 ) return e_invalidfont;
  111.        }
  112.        {    const ref_packed *pbe = pbbox->value.packed;
  113.         ref rbe[4];
  114.         int i;
  115.         for ( i = 0; i < 4; i++ )
  116.            {    packed_get(pbe, rbe + i);
  117.             pbe = packed_next(pbe);
  118.            }
  119.         if ( num_params(rbe + 3, 4, bbox) < 0 )
  120.             return e_invalidfont;
  121.        }
  122.     /* If no UniqueID entry, set the UniqueID member to -1, */
  123.     /* because UniqueID need not be present in all fonts, */
  124.     /* and if it is, the legal range is 0 to 2^24-1. */
  125.     if ( dict_find(op, &name_UniqueID, &puniqueid) <= 0 )
  126.         unique_id = -1;
  127.     else
  128.        {    if ( !r_has_type(puniqueid, t_integer) ||
  129.              puniqueid->value.intval < 0 ||
  130.              puniqueid->value.intval > ((1L << 24) - 1)
  131.            )
  132.             return e_invalidfont;
  133.         unique_id = puniqueid->value.intval;
  134.         /* Apparently fonts created by Fontographer often have */
  135.         /* a UniqueID of 0, contrary to Adobe's specifications. */
  136.         /* Treat 0 as equivalent to -1 (no UniqueID). */
  137.         if ( unique_id == 0 ) unique_id = -1;
  138.        }
  139.     code = build_gs_font(op, ppfont, ftype, pbuildchar);
  140.     if ( code != 0 ) return code;    /* invalid or scaled font */
  141.     pfont = *ppfont;
  142.     pfont->data.base.FontBBox.p.x = bbox[0];
  143.     pfont->data.base.FontBBox.p.y = bbox[1];
  144.     pfont->data.base.FontBBox.q.x = bbox[2];
  145.     pfont->data.base.FontBBox.q.y = bbox[3];
  146.     pfont->data.base.UniqueID = unique_id;
  147.     return 0;
  148. }
  149.  
  150. /* Do the common work for building a font of any FontType. */
  151. /* The caller guarantees that *op is a dictionary. */
  152. /* Return 0 for a new font, 1 for a font made by makefont or scalefont, */
  153. /* or a negative error code. */
  154. int
  155. build_gs_font(os_ptr op, gs_font **ppfont, font_type ftype, const ref *pbuildchar)
  156. {    ref *pftype;
  157.     ref *pmatrix;
  158.     gs_matrix mat;
  159.     ref *pencoding;
  160.     int wmode;
  161.     int code;
  162.     gs_font *pfont;
  163.     ref *pfid;
  164.     ref *aop = dict_access_ref(op);
  165.     if ( dict_find(op, &name_FontType, &pftype) <= 0 ||
  166.         !r_has_type(pftype, t_integer) ||
  167.         pftype->value.intval != (int)ftype ||
  168.         dict_find(op, &name_FontMatrix, &pmatrix) <= 0 ||
  169.         dict_find(op, &name_Encoding, &pencoding) <= 0 ||
  170.         read_matrix(pmatrix, &mat) < 0
  171.        )
  172.       return e_invalidfont;
  173.     switch ( r_type(pencoding) )
  174.        {
  175.     default: return e_invalidfont;
  176.     case t_array: case t_mixedarray: case t_shortarray: ;
  177.        }
  178.     code = dict_int_param(op, &name_WMode, 0, 1, 0, &wmode);
  179.     if ( code < 0 ) return code;
  180.     code = dict_find(op, &name_FID, &pfid);
  181.     if ( r_has_attr(aop, a_write) )
  182.        {    /* Assume this is a new font */
  183.         font_data *pdata;
  184.         if ( code > 0 ) return e_invalidfont;    /* has FID already */
  185.         if ( (pfont = (gs_font *)alloc(1, sizeof(gs_font), "buildfont(font)")) == 0 ||
  186.              (pdata = (font_data *)alloc(1, sizeof(font_data), "buildfont(data)")) == 0
  187.            )
  188.           return e_VMerror;
  189.         if ( (code = add_FID(op, pfont)) < 0 ) return code;
  190.         ref_assign(&pdata->dict, op);
  191.         ref_assign(&pdata->BuildChar, pbuildchar);
  192.         ref_assign(&pdata->Encoding, pencoding);
  193.         pfont->base = pfont;
  194.         pfont->dir = ifont_dir;
  195.         pfont->client_data = (char *)pdata;
  196.         pfont->FontType = ftype;
  197.         pfont->FontMatrix = mat;
  198.         pfont->WMode = wmode;
  199.         pfont->build_char_proc = gs_no_build_char_proc;
  200.        }
  201.     else
  202.        {    /* Assume this was made by makefont or scalefont */
  203.         if ( code <= 0 || !r_has_type(pfid, t_fontID) )
  204.             return e_invalidfont;
  205.         pfont = pfid->value.pfont;
  206.        }
  207.     *ppfont = pfont;
  208.     return 0;
  209. }
  210.